﻿using System;
using Data.implement;

namespace Data
{
    class ChargingRecordRepositery : DataAccessBase<LYWebsiteSEntities1>, IChargingRecordRepositery
    {
        private static readonly log4net.ILog log =
            log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        public bool InsertRecord(int userId, int targetId)
        {
            log.InfoFormat("ChargingRecordRepositery.InsertRecord({0},{1})",userId, targetId); 
            if(GetRecord(userId, targetId) != null)
                return false;

            Charging charging = new Charging()
            {
                CreateDate = DateTime.Now, UserId = userId, TargetId = targetId
                
            };
            charging = InsertRecord(charging);
            if (charging.TargetId <=0)
                return false;
            return true;
        }

        public bool DeleteRecord(Charging entity)
        {
            log.InfoFormat("ChargingRecordRepositery.DeleteRecord(userid{0},targetId{1})", entity.UserId, entity.TargetId);
            var result = GetRecordByProperty<Charging>(d=>d.UserId == entity.UserId && d.TargetId == entity.TargetId);
            return DeleteRecord(result);
        }

        public Charging GetRecord(int userId, int targetId)
        {
            log.InfoFormat("ChargingRecordRepositery.GetRecord({0},{1})", userId, targetId); 
            return GetRecordByProperty<Charging>(d=>d.UserId == userId && d.TargetId == targetId);
        }
    }
}